/**
 * 
 */
package com.netscenery.yang.model;

import org.jdom2.Element;

import com.netscenery.util.QName;
import com.netscenery.util.XMLTag;
import com.netscenery.yang.YangKeyword;
import com.netscenery.yang.YangStatement;
import com.netscenery.yang.designer.MapInfo;
import com.netscenery.yang.parser.YangParserException;

/**
 * @author chong
 *
 */
public abstract class Entity extends ModelElement {
	protected String description;
	protected String reference;
	protected Status status = Status.CURRENT;
	

	/**
	 * 
	 */
	public Entity() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param env
	 * @param name
	 * @throws ModelException
	 */
	public Entity(Env env, String name) throws ModelException {
		super(env, name);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param env
	 * @param qname
	 */
	public Entity(Env env, QName qname) {
		super(env, qname);
		// TODO Auto-generated constructor stub
	}
	
	

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getReference() {
		return reference;
	}

	public void setReference(String reference) {
		this.reference = reference;
	}

	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}


	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#readXML(org.jdom2.Element)
	 */
	@Override
	public int readXML(Element element) throws ModelException {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#readYin(org.jdom2.Element)
	 */
	@Override
	public int readYin(Element statement) throws ModelException {
		if(null == statement){
			return MODELELEMENT_OK;
		}
		if (statement.getName().equals(YangStatement.YANG_STATEMENT_DESCRIPTION)){
			Element text = statement.getChild(XMLTag.XML_TAG_TEXT,env.getNs());
			if (null == text){
				return MODELELEMENT_FAIL;
			}
			this.setDescription(text.getText());
		}
	    //reference		
		else if (statement.getName().equals(YangStatement.YANG_STATEMENT_REFERENCE)){
			
			Element text = statement.getChild(XMLTag.XML_TAG_TEXT,env.getNs());
			if (null == text){
				return MODELELEMENT_FAIL;
			}
			this.setReference(text.getText());
		}
	    //status
		else if (statement.getName().equals(YangStatement.YANG_STATEMENT_STATUS)){
			String value= statement.getAttributeValue(YangKeyword.VALUE.getKeyword());
			if (null == value){
				return MODELELEMENT_FAIL;
			}
			else {
				this.status = Status.getStatus(value);
			}
			
		}
		else {
			return MODELELEMENT_NOTFOUND;
		}
		return MODELELEMENT_OK;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#readYANG(com.netscenery.yang.YangStatement)
	 */
	@Override
	public int readYANG(YangStatement statement) throws YangParserException,
			ModelException {
		if(null == statement){
			return MODELELEMENT_OK;
		}
		
		String value = statement.getArgument();
		if (null == value){
			return MODELELEMENT_NOTFOUND;
		}
		if (statement.getKeyword().equals(YangStatement.YANG_STATEMENT_DESCRIPTION)){
			this.setDescription(value);
		}
	    //reference
		else if (statement.getKeyword().equals(YangStatement.YANG_STATEMENT_REFERENCE)){
			this.setReference(value);
		}
	    //status
		else if (statement.getKeyword().equals(YangStatement.YANG_STATEMENT_STATUS)){
			this.setStatus(Status.getStatus(value));
		}
		else {
			return MODELELEMENT_NOTFOUND;
		}
		
		return MODELELEMENT_OK;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#update()
	 */
	@Override
	public void update() {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#isFieldMandatory(java.lang.String)
	 */
	@Override
	public boolean isFieldMandatory(String field) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#getMapInfo()
	 */
	@Override
	public MapInfo getMapInfo() throws SecurityException, NoSuchMethodException {
		// TODO Auto-generated method stub
		return null;
	}

}
